package com.taotao.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.common.PictureResult;
import com.taotao.service.PictureService;
import com.taotao.util.FastDFSClient;

@Service
public class PictureServiceImpl implements PictureService {

	@Autowired
	FastDFSClient fastDFSClient;

	@Value("${IMAGE_SERVER_URL}")
	private String imageServerURL;

	@Override
	public PictureResult uploadPicture(MultipartFile picFile) {
		PictureResult result = new PictureResult();
		if (picFile.isEmpty()) {
			result.setError(1);
			result.setMessage("图片为空");
			return result;
		}
		// 上传图片到服务器
		try {
			String originalFilename = picFile.getOriginalFilename();
			String originalFilenameType = originalFilename
					.substring(originalFilename.lastIndexOf(".") + 1);
			String url = imageServerURL
					+ fastDFSClient.uploadFile(picFile.getBytes(),
							originalFilenameType);
			// 成功标志与图片的url
			result.setError(0);
			result.setUrl(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
